const path = require('path')
const bodyParser = require('body-parser') // post参数转换
const {
  shopAdmin,
  shopSay,
  shopRule,
  shopType,
  tableAdmin,
  shopKill,
  allshop,
  commodityList,
  killLookList,
<<<<<<< HEAD
  liveStreaming
=======
  groupShop
  
>>>>>>> cc2595f2b3d271eab31d8ddc81e4b30015bb2835
} = require('./src/mock/data') //来这里引入mock的数据
const port = 8888
const title = '电商后台'

function resolve(dir) {
  // 拼接当前文件所在目录和dir
  return path.join(__dirname, dir)
}

module.exports = {
  lintOnSave: false,
  devServer: {
    port,
    proxy: {
      '/api': {
        target: 'http://localhost:9003', //设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true, //這裡true表示实现跨域
        pathRewrite: {
          '^/api': '', //这里理解成用‘/api’代替target里面的地址，后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add'，直接写‘/api/user/add’即可
        },
      },
    },
    before: (app) => {
      //来这里写接口
      app.use(bodyParser.json()) // 处理post参数
      app.get('/allshop', (req, res) => {
        //当前页   每页条数  分类   关键字    状态
        let { pageIndex, pageSize, classId, keyword, status } = req.query
        let arrList = JSON.parse(JSON.stringify(allshop.list))
        //分类
        if (classId) {
          arrList = arrList.filter((item) => item.classId * 1 == classId * 1)
        }
        //搜索
        if (keyword) {
          arrList = arrList.filter(
            (item) =>
              item.keyword.includes(keyword) ||
              item.title.includes(keyword) ||
              item.id == keyword
          )
        }
        //状态
        if (status) {
          arrList = arrList.filter(item.status * 1 == status)
        }
        //分页
        if (pageIndex && pageSize) {
          arrList = arrList.slice(
            (pageIndex - 1) * pageSize,
            pageIndex * pageSize
          )
        }
        res.send({
          code: 1,
          data: arrList,
        })
      })
      //砍价列表
      app.get('/killLookList', (req, res) => {
        let newkillLookList = JSON.parse(JSON.stringify(killLookList.list))
        let { pageIndex, pageSize } = req.query
        if (pageIndex && pageSize) {
          newkillLookList = newkillLookList.slice(
            (pageIndex - 1) * pageSize,
            pageIndex * pageSize
          )
        }
        res.send({
          code: 1,
          data: newkillLookList,
          len: killLookList.list.length,
        })
      })
      app.post('/dev-api/user/login', (req, res) => {
        const { username } = req.body

        if (username === 'admin' || username === 'kkk') {
          res.json({
            code: 1,
            data: username,
          })
        } else {
          res.json({
            code: 10204,
            message: '用户名或密码错误',
          })
        }
      })
      app.get('/adminone', (req, res) => {
        // 分页 pageIndex pageSize
        // 返回 分割以后的数据 和 数据的总数
        const { pageIndex, pageSize, keyWord } = req.query
        const newList = shopAdmin
          .filter((item) => item.name.includes(keyWord || '')) // ‘’ 会返回所有数据  防止是undefined 导致一条数据都没有
          .slice((pageIndex - 1) * pageSize, pageIndex * pageSize) // splice 会修改原数组 slice 返回新数组
        res.send({
          code: 1,
          data: {
            list: newList,
            total: shopAdmin.length,
          },
        })
      })
      app.get('/tableAdmin', (req, res) => {
        res.send({
          code: 1,
          data: tableAdmin,
        })
      })
      app.get('/shopSort', (req, res) => {
        const { pageIndex, pageSize, keyWord } = req.query
        const newList = shopType
          .filter((item) => item.name.includes(keyWord || ''))
          .slice((pageIndex - 1) * pageSize, pageIndex * pageSize)
        res.send({
          code: 1,
          data: {
            list: newList,
            total: shopType.length,
          },
        })
      }),
        app.get('/shopSay', (req, res) => {
          res.send({
            code: 1,
            data: shopSay,
          })
        })
      app.get('/shopkill', (req, res) => {
        res.json({
          code: 1,
          data: shopKill,
        })
      })
      app.get('/dev-api/user/info', (req, res) => {
        const auth = req.headers['authorization']
        const roles = auth.split(' ')[1] === 'admin' ? ['admin'] : ['editor']
        res.json({
          code: 1,
          data: roles,
        })
      })
      app.get('/ruleone', (req, res) => {
        const { pageIndex, pageSize, keyWord } = req.query
        const newList = shopRule
          .filter((item) => item.name.includes(keyWord || ''))
          .slice((pageIndex - 1) * pageSize, pageIndex * pageSize)
        res.send({
          code: 1,
          data: {
            list: newList,
            total: shopRule.length,
          },
        })
      })
      app.get('/commodityList', (req, res) => {
        const { pageIndex, pageSize, id, keyword, flag } = req.query
        let newList = JSON.parse(JSON.stringify(commodityList))
        if (id) {
          newList = newList.filter((item) => item.id * 1 === id * 1)
        }
        // 模糊查询  名称 关键词 id
        if (keyword) {
          newList = newList.filter(
            (item) =>
              item.id === keyword ||
              item.title.includes(keyword) ||
              item.keyword.includes(keyword)
          )
        }
        // 状态查询
        if (flag) {
          newList = newList.filter((item) => item.flag * 1 === flag * 1)
        }
        // 分页
        if (pageIndex && pageSize) {
          newList = newList.slice(
            (pageIndex - 1) * pageSize,
            pageIndex * pageSize
          )
        }
        res.send({
          code: 1,
          total:
            flag * 1 === 1
              ? onSale
              : flag * 1 === 2
              ? warehouse
              : flag * 1 === 0
              ? sellOut
              : flag * 1 === -1
              ? recycleBin
              : commodityList.length, // 总数
          data: newList,
        })
      }),
      app.get('/groupShop', (req, res) => {
        const { pageIndex, pageSize, id, keyword, flag } = req.query
        let newList = JSON.parse(JSON.stringify(groupShop))
        if (id) {
          newList = newList.filter((item) => item.id * 1 === id * 1)
        }
        // 模糊查询  名称 关键词 id
        if (keyword) {
          newList = newList.filter(
            (item) =>
              item.id === keyword ||
              item.title.includes(keyword) ||
              item.keyword.includes(keyword)
          )
        }
        // 状态查询
        if (flag) {
          newList = newList.filter((item) => item.flag * 1 === flag * 1)
        }
        // 分页
        if (pageIndex && pageSize) {
          newList = newList.slice(
            (pageIndex - 1) * pageSize,
            pageIndex * pageSize
          )
        }
        res.send({
          code: 1,
          total:
            flag * 1 === 1
              ? onSale
              : flag * 1 === 2
                ? warehouse
                : flag * 1 === 0
                  ? sellOut
                  : flag * 1 === -1
                    ? recycleBin
                    : groupShop.length, // 总数
          data: newList,
        })
      })
      app.get("/liveStreaming", (req, res) => {
        const { pageIndex, pageSize, id, keyword, flag } = req.query;
        let liveList = JSON.parse(JSON.stringify(liveStreaming))

        if (id) {
          liveList = liveList.filter((item) => item.id * 1 === id * 1)
        }
        // 模糊查询  名称 关键词 id
        if (keyword) {
          liveList = liveList.filter(
            (item) =>
              item.id === keyword ||
              item.title.includes(keyword) ||
              item.name.includes(keyword)
          )
        }
        // 状态查询
        if (flag) {
          liveList = liveList.filter((item) => item.flag * 1 === flag * 1)
        }
        // 分页
        if (pageIndex && pageSize) {

          liveList = liveList.slice(
            (pageIndex - 1) * pageSize,
            pageIndex * pageSize
          )
        }
        res.send({
          code: 1,
          total:
            flag * 1 === 1
              ? onSale
              : flag * 1 === 2
                ? warehouse
                : flag * 1 === 0
                  ? sellOut
                  : flag * 1 === -1
                    ? recycleBin
                    : liveStreaming.length, // 总数
          data: liveList,
        })
      })

    },
  },
  configureWebpack: {
    name: title,
  },
  chainWebpack(config) {
    //修改文件引入自定义路径
    config.resolve.alias
      .set('@', path.resolve(__dirname, './src'))
      .set('@c', path.resolve(__dirname, './src/components'))
      .set('@css', path.resolve(__dirname, './src/assets/css'))
      .set('@img', path.resolve(__dirname, './src/assets/img'))
      .set('@js', path.resolve(__dirname, './src/assets/js'))
    // 1.修改svg规则，排除icons目录下的svg文件
    config.module.rule('svg').exclude.add(resolve('src/icons'))
    // 2.新增icons规则，仅打包icons目录下的svg文件
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({ symbolId: 'icon-[name]' })
      .end()
  },
}
